package dbobject

// 正常
const PersonStateNormal = 1
// 禁用
const PersonStateForbid = 2

type Person struct {
	Id            uint32
	Name          string
	OpenId        string
	Status        int8
}
func (p *Person) TableName() string {
	return "member__persons"
}
// 查询
func FindById(p *Person) error {
	query := "select id, name, status, open_id from member__persons where id = ?"
	row := db.QueryRow(query, p.Id)
	err := row.Scan(&p.Id, &p.Name, &p.OpenId, &p.Status)
	if err == sql.ErrNoRows {
		return errors.New("person not found")
	}
	return err
}
// 新增
func Insert(p *Person) (int64, error) {
	query := `insert into member__persons	(name, status, open_id) values (?, ?, ?)`
	result, err := db.Exec(query, p.Name, p.OpenId, p.Status)
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}
// 更新
func Update(p *Person) (int64, error) {
	query := "update member__persons set status = ? where id = ?"
	result, err := db.Exec(query, p.Status, p.Id)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}